angular.module('myApp',['ngRoute','ngCookies'])

//    该节示例可以查看https://www.w3cschool.cn/angularjs/angularjs-routing.html
//    w3cschool的AngularJS路由的教程
//    angularjs中文网的教程有错误，不能跑起来

//路由配置
.config(['$routeProvider',
    function ($routeProvider) {

        //$routeProvider提供路由service
        $routeProvider
            .when('/',{
                template:'this is index'
            })
            .when('/Book/:bookId', {
                templateUrl: 'book.html',
                controller: 'BookCtrl',
                controllerAs: 'book'
            })
            .when('/Book/:bookId/ch/:chapterId', {
                templateUrl: 'chapter.html',
                controller: 'ChapterCtrl',
                controllerAs: 'chapter'
            })
            .otherwise({
                redirectTo:'/'
            });
    }

])

// $cookies是一个对象，就是你的存储的cookie对象。

// $cookieStore也是一个对象，但是这个对象向你提供了三个操作cookie的接口，供我们使用。

// 这三个方法分别是：get、put、remove方法。

// 分别对应cookie操作的获取、设置、移除操作。

.controller('navController',['$route', '$routeParams', '$location','$cookies','$cookieStore',
    function($route, $routeParams, $location,$cookies,$cookieStore) {
        this.$route = $route;
        this.$location = $location;
        this.$routeParams = $routeParams;
        console.log($cookies);
        console.log($cookieStore);
        //设置cookie
        // $cookies.name = 'mapbar_front';
        $cookieStore.put('name','mapbar_front');
//获取cookie
        var name = $cookies[name];
        // var name = $cookieStore.get('name');
        console.log($cookies);
        console.log($cookieStore);
//移除cookie
        $cookieStore.remove("name");
//设置过期日期
        var time = new Date().getTime() + 5000;
        $cookieStore.put("cookie", "Hello wsscat", {
            expires: time
        });
    }
])

// Given:
// URL: http://server.com/index.html#/Chapter/1/Section/2?search=moby
// Route: /Chapter/:chapterId/Section/:sectionId
//
// Then
//$routeParams ==> {chapterId:'1', sectionId:'2', search:'moby'}

    .controller('BookCtrl', ['$routeParams', function($routeParams) {
        this.name = 'BookCtrl';
        this.params = $routeParams;
        console.log(this.params)
    }])

    .controller('ChapterCtrl', ['$routeParams', function($routeParams) {
        this.name = 'ChapterCtrl';
        this.params = $routeParams;
        console.log(this.params)
    }]);


